<template>
    <div id="v-left-b">
        <div class="car-out">
            <div style="width: 100%;height: 100%;" ref="checkChar"></div>
        </div>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted, watchEffect, reactive, toRaw, nextTick } from 'vue';
import * as echarts from 'echarts';
import ecTheme from "/@/assets/echartsConfig/westeros.project.json";
export default defineComponent({
    name:'v-left-b',
    props: {
        leftBtnData: {
            type: String,
            default: '',
        },
    },
    setup(props){
        const checkChar:Ref = ref(null);
        const liData = ref();
        let isFinish = false;

        echarts.registerTheme('westeros', ecTheme);
        const initCharts = () => {
            let chart: any; 
            chart = echarts.init(checkChar.value as HTMLElement, 'westeros');

            let option:any = {
                grid: {
                    left: '2%',
                    top:10,
                    width: '96%',
                    bottom: 0,
                    containLabel: true
                },
                tooltip: {},
                dataset: {
                    dimensions: ['日期', '单日数量'],
                    source: []
                },
                xAxis: { 
                    type: 'category',
                    axisLine:{
                        lineStyle: {
                            color:'#3f6986',
                        },
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        textStyle: {
                            color: "rgb(145,179,203)"
                        },
                        interval: 0
                    }
                },
                yAxis: {
                    splitLine: {
                        lineStyle: {
                            color: ['#3f6986']
                        }
                    },
                    axisLine:{
                        lineStyle: {
                            color:'#3f6986',
                        }
                    },
                    axisLabel: {
                        textStyle: {
                            color: "rgb(145,179,203)"
                        } 
                    },
                    min: 0,
                    max: 100,
                },
                series: [
                    { 
                        type: 'bar',
                        barMaxWidth: '34%',
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgb(0,253,236)' },
                                { offset: 1, color: 'rgb(1,37,35)' }
                            ])
                        }
                    },
                ]
            }
            let setTime = setInterval(() => {
                if(isFinish || liData.value){
                    let seriesData: any = [];
                    if(liData.value.xAxis.length !== 0){
                        liData.value.xAxis.map((i: any, inx: any) => {
                            let setArr: any = [];
                            setArr.push(i,liData.value.chart[0].count[inx] ? liData.value.chart[0].count[inx] : 0);
                            seriesData.push(setArr);
                        });
                    }
                    option.dataset.source = seriesData;
                    clearInterval(setTime);
                    return option && chart.setOption(option)
                }
            },20);
        }
        watchEffect(() => {
            let getData = props.leftBtnData;
            if(getData !== ''){
                let setArr = JSON.parse(getData);
                liData.value = setArr;
                isFinish = true;
                initCharts();
            }
        });
        onMounted(() => {
            nextTick(() => {
                initCharts()
            })
        })
        return {
            checkChar,
        }
    }
})
</script>
<style lang="scss" scoped>
#v-left-b{
    width: 100%;
    height: 100%;
    .car-out{
        margin: 6vh auto 0 auto;
        height: 72%;
        width: 90%;
    }
}
</style>